﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace Quartz.JobClass.Jobs
{
    public class ReportJiaoDuiJOB : IJob
    {
        public void Execute(IJobExecutionContext context)
        {
            try
            {
                //////获取报表清单 RP_GETREPORTLIST
                ////DataSet ds = null;//CommonTools.GetParamsDataSource("<PROCEDURE>", "SEL;RP_GETREPORTLIST;", "");
                ////if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                ////{
                ////    LogHelper.Info("开始调取数据...");
                ////    foreach (DataRow item in ds.Tables[0].Rows)
                ////    {
                ////        GetreportData(item["V_BAOBIAOID"].ToString(), item["V_CHAXUNGC"].ToString(), item["V_CHAXUNCS"].ToString(), item["V_HUOQUCS"].ToString(), item["V_FENZUID"].ToString());
                ////    }

                ////}
                ////else
                ////{
                ////    LogHelper.Info("无报表列可核对");
                ////    return;
                ////}
                int NY = 202009;
                SpHelper _SpHelper = new SpHelper();
                for (int i = 0; i < 100; i++)
                {
                    DataSet ds_cbsj = _SpHelper.GetParamsDataSource("<PROCEDURE>", "INSERT;GSC_ZWCHAOBIAOSJ;" + NY.ToString() + ";" + (i + 1).ToString(), "");
                    if (ds_cbsj.Tables[0].Rows.Count > 0)
                    {
                        string str = "csmsgd|331" + NY.ToString() + (i + 1).ToString().PadLeft(2, '0');
                        LogHelper.Info("开始处理：" + str);
                        byte[] byteArray = System.Text.Encoding.Default.GetBytes(str);
                        CDE_Engine.PumpChaoBiaoSJ(byteArray);
                    }
                }
                
            }
            catch (Exception ex)
            {
                LogHelper.Info("数据生成异常" + ex.Message);
            }
            
        }
        public void GetreportData(string i_baobiaoid,string s_storesname,string chaxuncs,string huoqucs,string fenzuid)
        {
            
            //获取数据库时间
            DateTime databasetime = Convert.ToDateTime(new SpHelper().GetParamsDataSource("<PROCEDURE>", "SEL;COMMON_GETDATABASETIME;", "").Tables[0].Rows[0]["V_TIME"]);
            string strParams = "";
            foreach (string item in chaxuncs.Split('|'))//获取存过程调取参数
            {
                if (item == "<YEAR>")
                {
                    strParams += databasetime.Year + ";";
                }
                else if (item == "<MONTH>")
                {
                    strParams += databasetime.Month + ";";
                }
                else if (item == "<DAY>")
                {
                    strParams += databasetime.Day + ";";
                }
                else
                {
                    strParams += item + ";";
                }
                
            }
            strParams = strParams.Substring(0, strParams.Length - 1);
            LogHelper.Info("报表ID{0}，执行存储过程{1}，参数值{2}", i_baobiaoid, s_storesname, strParams);
            DataSet ds = new SpHelper().GetParamsDataSource("<PROCEDURE>", "SEL;" + s_storesname + ";" + strParams, "");

            string file = huoqucs.Split('|')[0];
            DataRow[] dr_values = ds.Tables[0].Select(file,"");

            string insParams="";
            insParams += i_baobiaoid + ";";//V_I_BAOBIAOID
            insParams += strParams.Replace(';','&') + ";";//V_S_CHAXUNCS
            insParams += huoqucs.Split('|')[0].Split('=')[0] + ";";//V_S_FILTER
            insParams += huoqucs.Split('|')[0].Split('=')[1] + ";";//V_S_HUOQUCS
            insParams += dr_values[0]["" + huoqucs.Split('|')[1] + ""] + ";";//V_VALUE
            insParams += fenzuid + ";";//V_I_FENZUID
            insParams += databasetime.Year + ";";//V_CHAOBIAON
            insParams += databasetime.Month + "";//V_CHAOBIAOY
            DataSet ds_result = new SpHelper().GetParamsDataSource("<PROCEDURE>", "INS;RP_GETREPORTDATA;" + insParams, "");

        }
    }
}
